package cn.icanci.loopstack.bic.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.biz.mapper.BuryingMapper;
import cn.icanci.loopstack.bic.biz.service.BuryingService;
import cn.icanci.loopstack.bic.biz.service.SystemService;
import cn.icanci.loopstack.bic.biz.service.UseCaseService;
import cn.icanci.loopstack.bic.common.model.BuryingVO;
import cn.icanci.loopstack.bic.common.model.base.SystemVO;
import cn.icanci.loopstack.bic.common.model.base.UseCaseVO;
import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.BuryingDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.BuryingDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/08/19 15:40
 */
@Service
public class BuryingServiceImpl implements BuryingService {
    @Resource
    private BuryingDAO     buryingDAO;
    @Resource
    private BuryingMapper  buryingMapper;
    @Resource
    private SystemService  systemService;
    @Resource
    private UseCaseService useCaseService;

    @Override
    public void batchInsert(List<BuryingVO> buryingList) {
        if (CollectionUtils.isEmpty(buryingList)) {
            return;
        }
        buryingDAO.batchInsert(buryingMapper.vos2dos(buryingList));
    }

    @Override
    public List<BuryingVO> queryAll() {
        return buryingMapper.dos2vos(buryingDAO.queryAll());
    }

    @Override
    public PageList<BuryingVO> queryPage(BuryingVO burying, int pageNum, int pageSize) {
        PageList<BuryingDO> pageQuery = buryingDAO.pageQuery(buryingMapper.vo2do(burying), pageNum, pageSize);
        return new PageList<>(buryingMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());
    }

    @Override
    public void save(BuryingVO burying) {
        if (doInsert(burying)) {
            buryingDAO.insert(buryingMapper.vo2do(burying));
        } else {
            buryingDAO.update(buryingMapper.vo2do(burying));
        }
    }

    @Override
    public List<BuryingVO> queryBySystemAndUseCase(String systemUuid, String useCaseUuid, String traceId, String orderSerialNo) {
        SystemVO systemVO = systemService.queryByUuid(systemUuid);
        UseCaseVO useCaseVO = useCaseService.queryBySystemAndUseCase(systemUuid, useCaseUuid);

        return buryingMapper.dos2vos(buryingDAO.queryBySystemAndUseCase(systemVO.getSystemKey(), useCaseVO.getUseCaseKey(), traceId, orderSerialNo));
    }

    private boolean doInsert(BuryingVO burying) {
        return StringUtils.isBlank(burying.getId());
    }
}
